﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Extend_CollectLinkInfoController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回采集列表列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            string sort = (string)Utils.GetQueryAndFormString("sort", this, 100, 0, DataType.Str);
            QueryParam qp = new QueryParam();
            if (!string.IsNullOrEmpty(sort))
            {
                SortEntity sortEntity = null;
                List<SortEntity> sortList = new List<SortEntity>();
                sortList = (List<SortEntity>)JsonConvert.DeserializeObject(sort, typeof(List<SortEntity>));
                if (sortList.Count > 0)
                {
                    sortEntity = sortList[0];
                }

                qp.PageIndex = pageIndex / pageSize + 1;
                qp.PageSize = pageSize;
                if (sortEntity != null)
                    qp.Orderfld = string.Concat(sortEntity.Property, " ", sortEntity.Direction);
                qp.Where = "CollectSetId=" + actionId;
            }
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Extend_CollectLinkInfoEntity> Extend_CollectLinkInfoSv = ServiceBuilder.BuildExtend_CollectLinkInfoService();
            List<Extend_CollectLinkInfoEntity> extend_collectlinkinfoList = Extend_CollectLinkInfoSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(extend_collectlinkinfoList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看采集列表 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Extend_CollectLinkInfoEntity> Extend_CollectLinkInfoSv = ServiceBuilder.BuildExtend_CollectLinkInfoService();
           int intId = Utils.StringToInt(actionId, 0);
            Extend_CollectLinkInfoEntity extend_collectlinkinfo = Extend_CollectLinkInfoSv.Look(intId);
            if (extend_collectlinkinfo != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(extend_collectlinkinfo), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新采集列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Extend_CollectLinkInfoEntity extend_collectlinkinfo = new Extend_CollectLinkInfoEntity();
                extend_collectlinkinfo.DataTable_Action = DataTableAction.Update;
                extend_collectlinkinfo.Id = intId;
                extend_collectlinkinfo.IsCollect = (bool)Utils.GetQueryAndFormString("IsCollect", this, 1,  1 , DataType.Bool);
                extend_collectlinkinfo.Url = (String)Utils.GetQueryAndFormString("Url", this, 550,  0, DataType.Str);
                extend_collectlinkinfo.CollectSetId = (Int32)Utils.GetQueryAndFormString("CollectSetId", this, 10,  1 , DataType.Int);
                extend_collectlinkinfo.CollectNum = (Int32)Utils.GetQueryAndFormString("CollectNum", this, 10,  0, DataType.Int);
                IDataService<Extend_CollectLinkInfoEntity> Extend_CollectLinkInfoSv = ServiceBuilder.BuildExtend_CollectLinkInfoService();
                if (Extend_CollectLinkInfoSv.Update(extend_collectlinkinfo) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增采集列表
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Extend_CollectLinkInfoEntity extend_collectlinkinfo = new Extend_CollectLinkInfoEntity();
            extend_collectlinkinfo.DataTable_Action = DataTableAction.Insert;
            extend_collectlinkinfo.IsCollect = (bool)Utils.GetQueryAndFormString("IsCollect", this, 1, 1, DataType.Bool);
            extend_collectlinkinfo.Url = (String)Utils.GetQueryAndFormString("Url", this, 550,  0, DataType.Str);
            extend_collectlinkinfo.CollectSetId = (Int32)Utils.GetQueryAndFormString("CollectSetId", this, 10,  1 , DataType.Int);
            extend_collectlinkinfo.CollectNum = (Int32)Utils.GetQueryAndFormString("CollectNum", this, 10,  0, DataType.Int);
           IDataService<Extend_CollectLinkInfoEntity> Extend_CollectLinkInfoSv = ServiceBuilder.BuildExtend_CollectLinkInfoService();
           if (Extend_CollectLinkInfoSv.Insert(extend_collectlinkinfo) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除采集列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Extend_CollectLinkInfoEntity extend_collectlinkinfo = new Extend_CollectLinkInfoEntity();
                extend_collectlinkinfo.Id = intId;
                IDataService<Extend_CollectLinkInfoEntity> Extend_CollectLinkInfoSv = ServiceBuilder.BuildExtend_CollectLinkInfoService();
                if (Extend_CollectLinkInfoSv.Delete(extend_collectlinkinfo) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除采集列表
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Extend_CollectLinkInfoEntity> Extend_CollectLinkInfoSv = ServiceBuilder.BuildExtend_CollectLinkInfoService();
                if (Extend_CollectLinkInfoSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            } 
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
